/**
 * @description 菜单管理控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const dayjs = require("dayjs")
const menu_model = require("../../../model/system/menu/MENU.MODEL")
const { getFilter } = require("../../../utils/setFilter")

// 创建一个内容
const menu_controller_save = async ({
    id = undefined,
    visible = undefined,
    parentId = undefined,
    menuName = undefined,
    menuType = undefined,
    menuIcon = undefined,
    routerPath = undefined,
    sort = undefined,
    componentName = undefined,
    componentPath = undefined,
    permissionMark = undefined,
    status = undefined,
    activeMenu = undefined,
    askBeforeClose = undefined,
}) => {
    const params = {
        createDate: dayjs(),

        visible,
        parentId,
        menuName,
        menuType,
        menuIcon,
        routerPath,
        sort,
        componentName,
        componentPath,
        permissionMark,
        status,
        activeMenu,
        askBeforeClose
    }
    let addRes = null;
    if (!id) {
        // 添加创建
        addRes = await menu_model.create({
            id: randomUUID(),
            ...params
        })
    } else {
        // 更新修改
        addRes = await menu_model.findOneAndUpdate({ id: id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            id,
            parentId,
            visible,
            menuName,
            menuType,
            menuIcon,
            routerPath,
            sort,
            componentName,
            componentPath,
            permissionMark,
            status,
            activeMenu,
            askBeforeClose
        } = addRes

        return {
            code: 0,
            message: "成功",
            data: {
                id,
                parentId,
                visible,
                menuName,
                menuType,
                menuIcon,
                routerPath,
                sort,
                componentName,
                componentPath,
                permissionMark,
                status,
                activeMenu,
                askBeforeClose
            }
        }
    }
    return {
        status: "2000",
        message: "失败",
    }
}




// 查询列表 
const menu_controller_find = async ({ menuName = null, pageSize = 1000, current = 1 }) => {
    try {
        let filter = {
            menuName: menuName && { $regex: eval("/" + menuName + "/i") },//名称模糊查询
            // status: 1
        }
        filter = getFilter(filter)
        const list = await menu_model.find(filter).sort({ sort: 1 }).lean().skip((current - 1) * pageSize).limit(pageSize)

        const totalCount = await menu_model.countDocuments(filter)
        console.log("一共多少数据？", totalCount)
        return {
            list,
            totalCount
        }
    } catch (error) {
        console.log("控制器失败", error)
        return {
            code: 12,
            msg: "查询失败"
        }
    }
}


/**@description 根据id查详情 */
const menu_controller_get = async ({ id }) => {
    const res = await menu_model.findOne({
        id
    })

    console.log("菜单详情", res);
    if (res) {
        const { id, parentId, menuName, menuType, menuIcon, routerPath, sort, componentName, componentPath, permissionMark, status, visible, activeMenu,askBeforeClose } = res;

        return {
            code: 0,
            msg: "查询成功",
            data: {
                id, parentId, menuName, menuType, menuIcon, routerPath, sort, componentName, componentPath, permissionMark, status, visible, activeMenu,askBeforeClose
            }
        }
    } else {
        return {
            code: '01',
            msg: "未找到对应菜单数据"
        }
    }
}

// 删除
const menu_controller_delete = async ({ ids = [] }) => {
    const deleteResult = await menu_model.deleteMany({
        id: { $in: ids }
    });

    if (deleteResult.deletedCount) {
        return {
            code: 0,
            msg: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            code: 12,
            msg: `删除失败`
        }
    }

}
module.exports = {
    menu_controller_save,
    menu_controller_find,
    menu_controller_get,
    menu_controller_delete,
}